
#include "magic_enum.hpp"

namespace hms
{

template<typename E>
void readEnumOrDefault(
	const ini::INIReader& reader,
	const std::string& section,
	const std::string& item,
	E& defaultVal
){
	using namespace magic_enum;

	std::string defaultValStr { enum_name(defaultVal) };

	/* get value or default */
	std::string valStr { reader.Get(section, item, defaultValStr ) };

	/* try to cast to enum, use default on failure and print warning */
	std::optional<E> val { enum_cast<E>(valStr) };
	if ( !val ){
		val = defaultVal;
		log::warn(
			"Unknown option: \"[{}] {}={}\""
			"\nValid options are:{}"
			"\nDefaulting to {}",
			section, item, bold(yellow(valStr)),
			// fmt::to_string(options),
			enumValuesToString<E>(),
			bold(defaultValStr)
		);
	} else {
		defaultVal = val.value();
	}
}

template<typename E>
E readEnumOrDefault(
	const ini::INIReader& reader,
	const std::string& section,
	const std::string& item
){
	E val { magic_enum::enum_value<E>(0) };
	readEnumOrDefault(reader, section, item, val);
	return val;
}

} // namespace hms
